package cn.doctorpda.conference.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import org.apache.commons.logging.Log;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.User;
import cn.doctorpda.conference.localstore.ClientInfo;
import cn.doctorpda.conference.presenter.LoginPresenter;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.ui.splash.SplashActivity;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.ILoginView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/6/6.
 */
public class SwitchAccountFragment extends BaseFragment implements ILoginView<User> {

    private String username, password;
    private int flag;

    public static SwitchAccountFragment newInstance(String username, String password, int flag) {
        SwitchAccountFragment fragment = new SwitchAccountFragment();
        Bundle bundle = new Bundle();
        bundle.putString("username", username);
        bundle.putString("password", password);
        bundle.putInt("flag", flag);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        username = getArguments().getString("username");
        password = getArguments().getString("password");
        flag = getArguments().getInt("flag");

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_switch_account, container, false);
        thirdLogin();
        return view;
    }

    void toThird() {
        if (flag == 2) {
            toMain();
        } else if (flag == 4) {
            //进入各种详情页面
            if (TextUtils.isEmpty(ClientInfo.getClientId(getActivity()))) {
                toMain();
            } else {
                Bundle extras = getActivity().getIntent().getExtras();
                String id = extras.getString("id");
                String type = extras.getString("type");
                if (TextUtils.isEmpty(type)) {
                    toMain();
                }
                if (TextUtils.equals(type, "con")) {
                    //进入会议详情页
                    String url = extras.getString("url");
                    if (TextUtils.isEmpty(url)) {
                        //url 为空 进入会议首页
                        toMain();
                    } else {
                        DetailActivity.startActivity(getActivity(), "详情", id, type, url);
                        getActivity().finish();
                    }
                }
            }
        } else {
            toMain();
        }
    }

    private void thirdLogin() {
        // TODO 登录接口, 当登录成功后 isSuccess = true；
        LoginPresenter loginPresenter = new LoginPresenter(this);

        loginPresenter.getLoginData(username, password);
    }

    void toMain() {
        android.util.Log.i("hongshi","=====");
        startActivity(new Intent(getActivity(), SplashActivity.class));
        getActivity().finish();
    }

    @Override
    public String getPageName() {
        return "SwitchAccountFragment";
    }

    @Override
    public void setLoginResult(Msg msg, User data) {
        L.d("setLoginResult");
//        T.showShort(getContext(), "登陆成功");
        if (msg.isSuccess()) {
            T.showShort(getActivity(), "切换用户成功");
            toThird();
        } else {
            L.d("setLoginResultfail");
            T.showShort(getActivity(), "切换用户失败，您将进入主页");
            toThird();
        }
    }

    @Override
    public void setILoginToast(Msg msg) {

    }

    @Override
    public void setLoginError(Call call, Exception e) {
        toMain();
    }
}
